<?php
namespace app\admin\controller;
use app\model\WxMember as WxMemberModel;
use think\Db;


class WxMember extends Base
{
    public function __construct(){
        $model = new WxMemberModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $categorys = Db::name('wx_member_category')->select();
        $categorys = arrayParamExtract($categorys,'id','name');

        $assistants = Db::name('wx_assistant')->select();
        $assistants = arrayParamExtract($assistants,'id','name');

        $users = Db::name('user')->select();
        $users = arrayParamExtract($users,'id','nickname');

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'member_category_id'=>['name'=>'分类','type'=>'select1','params'=>$categorys],
            'image'=>['name'=>'图片','type'=>'file'],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'position'=>['name'=>'职位','type'=>'text'],
            'company'=>['name'=>'公司','type'=>'text'],
            'description'=>['name'=>'个人简介','type'=>'textarea'],
            'resource'=>['name'=>'资源','type'=>'textarea'],
            'requirement'=>['name'=>'需求','type'=>'textarea'],
            'view_num'=>['name'=>'浏览数','type'=>'text'],
            'wx_assistant_id'=>['name'=>'绑定助理','type'=>'select1_1','params'=>$assistants],
            'user_id'=>['name'=>'绑定用户','type'=>'select1_1','params'=>$users],
            'tags'=>['name'=>'标签','type'=>'text','placeholder'=>'多个用 , 分割'],
            // 'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
            
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['category'] = Db::name('wx_member_category')->field('name')->where('id',$vo['member_category_id'])->find();
            $list['data'][$k]['assistant'] = Db::name('wx_assistant')->field('name')->where('id',$vo['wx_assistant_id'])->find();
            
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','member_category_id','image','description'],
                'nomust'=>['position','company','resource','requirement','view_num','sort','wx_assistant_id','user_id']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','member_category_id','image','description'],
                'nomust'=>['position','company','resource','requirement','view_num','sort','wx_assistant_id','user_id']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }




   
}
